{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 求解一元二次方程\n",
    "### 要求：面向过程/面向对象\n",
    "### 程序和可执行文件打包压缩后上传，文件名为：周蔚生_22019207006_作业1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 面向过程"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请依次输入一元二次方程的三个参数a，b，c :\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "a: 1\n",
      "b: 2\n",
      "c: 1\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a = 1.0 \n",
      "b = 2.0 \n",
      "c = 1.0\n",
      "一元二次方程为： x² + 2x + 1 = 0\n",
      "方程有两个相同的解,x= -1.0\n"
     ]
    }
   ],
   "source": [
    "print('请依次输入一元二次方程的三个参数a，b，c :')\n",
    "\n",
    "a = float(input('a:'))\n",
    "b = float(input('b:'))\n",
    "c = float(input('c:'))\n",
    "print('a =',a,'\\nb =',b,'\\nc =',c)\n",
    "print(\"一元二次方程为：{}x² + {:.0f}x + {:.0f} = 0\".format(\" \" if a==1 else a ,b,c))\n",
    "\n",
    "delta = b*b - 4*a*c\n",
    "x1 = (-b+delta**0.5)/(2*a)\n",
    "x2 = (-b-delta**0.5)/(2*a)\n",
    "# print(delta)\n",
    "\n",
    "if delta == 0:\n",
    "        print(\"方程有两个相同的解,x=\",-b/(2*a))\n",
    "elif delta < 0:\n",
    "        print(\"原方程无实根。\")\n",
    "else:\n",
    "        print(\"方程有两个根分别为x1={:.3f},x2={:.3f}。\".format(x1,x2))\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 面向对象"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请依次输入一元二次方程的三个参数a，b，c :\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "a: 1\n",
      "b: 4\n",
      "c: 5\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a = 1.0 \n",
      "b = 4.0 \n",
      "c = 5.0\n",
      "一元二次方程为： x² + 4x + 5 = 0\n",
      "原方程无实根。\n"
     ]
    }
   ],
   "source": [
    "class SolutionOfEqu:\n",
    "    def __init__(self):\n",
    "          pass\n",
    "        \n",
    "    def input_(self):\n",
    "        print('请依次输入一元二次方程的三个参数a，b，c :')\n",
    "\n",
    "        self.a = float(input('a:'))\n",
    "        self.b = float(input('b:'))\n",
    "        self.c = float(input('c:'))\n",
    "        print('a =',self.a,'\\nb =',self.b,'\\nc =',self.c)\n",
    "        print(\"一元二次方程为：{}x² + {:.0f}x + {:.0f} = 0\".format(\" \" if self.a==1 else self.a ,self.b,self.c))\n",
    "    def quadratic(self):\n",
    "        \n",
    "        delta = self.b*self.b - 4*self.a*self.c\n",
    "        x1 = (-self.b+delta**0.5)/(2*self.a)\n",
    "        x2 = (-self.b-delta**0.5)/(2*self.a)\n",
    "\n",
    "        if delta == 0:\n",
    "                print (\"方程有两个相同的解,x=\",-self.b/(2*self.a))\n",
    "        elif delta < 0:\n",
    "                print(\"原方程无实根。\")\n",
    "        else:\n",
    "                print(\"方程有两个根分别为x1={:.2f},x2={:.2f}。\".format(x1,x2))\n",
    "  \n",
    "\n",
    "if __name__ == '__main__':\n",
    "    k1 = SolutionOfEqu()\n",
    "    k1.input_()\n",
    "    k1.quadratic()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请依次输入一元二次方程的三个参数a，b，c :\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "a: 1\n",
      "b: 4\n",
      "c: 3\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a = 1.0 \n",
      "b = 4.0 \n",
      "c = 3.0\n",
      "一元二次方程为： x² + 4x + 3 = 0\n",
      "方程有两个根分别为x1=-1.00,x2=-3.00。\n"
     ]
    }
   ],
   "source": [
    "if __name__ == '__main__':\n",
    "    k2 = SolutionOfEqu()\n",
    "    k2.input_()\n",
    "    k2.quadratic()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
